{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import tushare as ts\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import os\n",
    "\n",
    "ts.set_token('17c568eceed18741d63878c0605b609a9170010291ddd99e16fd566f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "now:  2009-01-05 09:30:00 his:  2008-05-20 09:30:00\n"
     ]
    }
   ],
   "source": [
    "if os.path.exists('601318.SH_5min.csv'):\n",
    "    data = pd.read_csv('601318.SH_5min.csv', index_col=0)\n",
    "    now = datetime.datetime.strptime(data.trade_time.iloc[-1], '%Y-%m-%d %H:%M:%S')\n",
    "    his = now - datetime.timedelta(days=230)\n",
    "else:\n",
    "    data = pd.DataFrame()\n",
    "    now = datetime.datetime.now()\n",
    "    his = now - datetime.timedelta(days=230)\n",
    "\n",
    "print('now: ', now, 'his: ', his)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 2009-01-05 09:30:00 (0, 8)\n",
      "1 2008-05-20 09:30:00 (0, 8)\n",
      "2 2007-10-03 09:30:00 (0, 8)\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    new_data = ts.pro_bar(ts_code='601318.SH', adj='qfq', freq='5min',\n",
    "                              start_date=his.strftime('%Y%m%d'), end_date=now.strftime('%Y%m%d'))\n",
    "    if new_data is None:\n",
    "        break\n",
    "    print(i, now, new_data.shape)\n",
    "    data = data.append(new_data)\n",
    "    now, his = his, his - datetime.timedelta(days=230)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_csv('601318.SH_5min.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2009-01-05 09:30:00'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.trade_time.iloc[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(128576, 8)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "toc": {
   "base_numbering": 1.0,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
